import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-login-log',
	templateUrl: './login-log.component.html',
})
export class LoginLogComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	tableParam:any = {
		searchKey: '',
		startTime: '',
		endTime: ''
	}
	isShowDelete=false; // 默认不显示删除按钮
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	ngOnInit() {
		this.initBtn();
		this.load();
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_wx:bind:login')));
		if(!btnData) {
			return
		};
		for(let item of btnData) {
			if(item.sourceKey === "wx-bind:login-log:deleteBtn" && item.parentId) {
				this.isShowDelete = true;
			}
		}
	}
	//table刷新
    refresh() {
    	this.load();
    }
	//表格搜索
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
    //分页回调
    paginate(e){
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
	//初始化加载list数据
	load(){
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				searchKey:common.stringTrim(this.tableParam.searchKey),
				startTime: this.tableParam.startTime,
				endTime: this.tableParam.endTime
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("appointmentRepairCar/loginLogList.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.tableParam.startTime = time.startTime;
		this.tableParam.endTime = time.endTime;
	} 
	// 删除登录日志
	deleteRow(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let parameters = {
				ids: row.id,
				type: "3",
			}
			this.jsonApiService.http_obj("appointmentRepairCar/batchDeleted.html", parameters).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
}